package com.zosean.sky.main;

import javax.microedition.lcdui.*;
import com.zosean.sky.stringItem.*;
import java.io.DataInputStream;
import java.util.Hashtable;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author sky
 * @version 1.0
 */
public class MainCanvas extends Canvas implements CommandListener, Runnable
{
    
    private Thread mainThread;
    private static final String TEMPSTRING = "tempString";
    private int tempX;
    private int tempY;
    private long endSystemTime;
    private BitTextsManage btm;
    private PaintBitMap pbm = new PaintBitMap();
    public MainCanvas()
    {
        try
        {
            jbInit();
                
            mainThread = new Thread(this);
            mainThread.start();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception
    {
        setFullScreenMode(false);
        endSystemTime = System.currentTimeMillis();
        btm = new BitTextsManage("/locale.txt", "fontImage", 18, 14);
        btm.setBitMap();
        System.out.println("Consumption Time = " + (System.currentTimeMillis() - endSystemTime));
    }

    public void commandAction(Command command, Displayable displayable)
    {
    }

    protected void paint(Graphics g)
    {
        /** @todo Add paint codes */
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0xff0000);
        g.drawString(TEMPSTRING, tempX, tempY, 0);
//        g.drawImage((Image)btm.get_HtStringToImage().get("于"), 0, 220, 0);
//        g.drawImage((Image)btm.get_HtStringToImage().get("关"), 20, 220, 0);
//        pbm.bitPaint(g, "设置倾斜世界", btm.get_HtStringToImage(), 0, 200);
        btm.drawBitmap(g, "更多关于帮助", 0, 100, 200);
    }
    protected void keyPressed(int key)
    {
        btm.setSpace(0);
//        btm.
    }
    protected void keyReleased(int key)
    {
        
    }
    public void run()
    {
        while (true)
        {
            try
            {
                Thread.sleep(100);
                repaint();
            } catch (Exception ex)
            {
//                System.out.println("RUN ERROR" + ex.toString());
                ex.printStackTrace();
            }
            tempX++;
            tempY++;
        }
    }

    //************************
    public String readFile(String fileName)
    {
        DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(fileName));
        int initLength = 3;
        int expansionLength = 3;
        byte[] returnByteArray = new byte[expansionLength];
        byte[] tempStorByteArray;
        String return_String = null;
        try
        {
            int ch; // 每次读出来的字符
            int currentIndex = 0;
            while ((ch = dis.read()) != -1)
            {
//                if(ch == ' ')
//                    continue;
                returnByteArray[currentIndex] = (byte) ch;
                currentIndex++;
                if (currentIndex >= expansionLength)
                {
                    initLength += expansionLength;
                    tempStorByteArray = new byte[initLength];
                    System.arraycopy(returnByteArray, 0, tempStorByteArray, 0, currentIndex);
                    returnByteArray = null;
                    returnByteArray = tempStorByteArray;
                }
            }
            if (initLength % expansionLength == 0)
            {
                tempStorByteArray = new byte[currentIndex];
                System.arraycopy(returnByteArray, 0, tempStorByteArray, 0, currentIndex);
                returnByteArray = null;
                returnByteArray = tempStorByteArray;
            }
            return_String = new String(returnByteArray, "UTF-8");
            if (dis != null)
            {
                dis.close();
                dis = null;
            }
        } catch (Exception e)
        {
            System.out.println("READ FILE ERROR :" + e.toString());
        }
        return return_String;
    }

}
